package com.yuji.aacar.controller.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.Const;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.bean.beans.OrderList;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.widgets.autolayout.AutoLinearLayout;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import java.util.ArrayList;


/**
 * <li>Package:com.yuji.aacar.controller.adapter</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/2</li>
 * <li>Description:   </li>
 */

public class OrderHomeAdapter extends RecyclerView.Adapter<OrderHomeAdapter.ViewHolder> {

    Activity context;
    private ArrayList<OrderList> mData;
    protected LayoutInflater mInflater;


    public OrderHomeAdapter(Activity context, ArrayList<OrderList> data) {
        this.context = context;
        this.mData = data;
        mInflater = LayoutInflater.from(context);
    }

    public void updateData(ArrayList<OrderList> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.fragment_order_home_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        OrderList orderList = mData.get(position);
//        holder.orderList = orderList;
        holder.orderNumber.setText(orderList.getOrderNo());
        holder.orderStatus.setText(orderList.getOrderStautsName());
        holder.productPrice.setText("¥"+orderList.getPayAmount());

        String type = orderList.getWashType();
        if (type.contentEquals(Const.WASH_TYPE_IMMEDIATE+"")){//及时洗
            holder.productImage.setImageResource(R.mipmap.icon_immediate);
            holder.productName.setText("即时洗车");
        } else {
            holder.productImage.setImageResource(R.mipmap.icon_reserve);
            holder.productName.setText("预约洗车");
        }
        String status = orderList.getOrderStatus();//订单状态(R01-待支付 R02-待接单 R03-已接单 R04-洗车中 R05-待评价 R06-已完成)
        if (status.contentEquals("R01")){
            holder.pay.setVisibility(View.VISIBLE);
        } else if (status.contentEquals("R02")){

        } else if (status.contentEquals("R03")){

        } else if (status.contentEquals("R04")){

        } else if (status.contentEquals("R05")){
//            holder.comment.setVisibility(View.VISIBLE);
            holder.buyAgainLayout.setVisibility(View.VISIBLE);
        } else if (status.contentEquals("R06")){
             holder.buyAgainLayout.setVisibility(View.VISIBLE);
        } else if (status.contentEquals("R07")){

        } else if (status.contentEquals("R08")){
             holder.buyAgainLayout.setVisibility(View.VISIBLE);
        }

        Intent intent = context.getIntent();


        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("orderNo", orderList.getOrderNo());
                int a = Integer.parseInt(orderList.getWashType());
                intent.putExtra("washType", a);
                CoreEventCenter.postObject(EventConst.ORDER_ITEM, intent);
            }
        });

        holder.pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("orderNumber", orderList.getOrderNo());
                intent.putExtra("amount",  orderList.getResultProductPrice());

                LogUtil.e("--pay--", orderList);
                CoreEventCenter.postObject(EventConst.ORDER_PAY, intent);
            }
        });
        holder.buyAgainLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("orderType", Const.ORDER_BUY_AGAIN);
                int a = Integer.parseInt(orderList.getWashType());
                intent.putExtra("washType", a);
                intent.putExtra("orderno", orderList.getOrderNo());
//                intent.putExtra("amount",  orderList);
                CoreEventCenter.postObject(EventConst.ORDER_BUY_AGAIN, intent);
            }
        });
//        holder.orderNumber.setText(orderList.getOrderNo());
//        holder.orderNumber.setText(orderList.getOrderNo());
//        holder.layout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Toast.makeText(mContext, "pos:" + position, Toast.LENGTH_SHORT).show();
//                AppRoute.getInstance().nextController(context, OrderDetailActivity.class, AppRoute.WITHOUT_RESULTCODE);
//                LogUtil.e("item", "item");
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        private OrderList orderList;
        private AutoLinearLayout layout;
        private PingFangTextView orderNumber;
        private PingFangTextView orderStatus;
        private ImageView productImage;
        private PingFangTextView productName;
        private TextView productPrice;
        private AutoLinearLayout buyAgainLayout;//再次购买
        private Button pay;//立即支付
        private AutoLinearLayout comment;//评价


        public ViewHolder(View itemView) {
            super(itemView);
            layout = (AutoLinearLayout) itemView.findViewById(R.id.order_home_item_linerlayout);
            orderNumber = (PingFangTextView) itemView.findViewById(R.id.order_home_order_number_tv);
            orderStatus = (PingFangTextView) itemView.findViewById(R.id.order_home_order_status_tv);
            productImage = (ImageView) itemView.findViewById(R.id.order_home_item_image);
            productName = (PingFangTextView) itemView.findViewById(R.id.order_home_item_name);
            productPrice = (TextView) itemView.findViewById(R.id.order_home_item_price);

            buyAgainLayout = (AutoLinearLayout) itemView.findViewById(R.id.order_home_item_buy_again);
            pay = (Button) itemView.findViewById(R.id.order_home_item_pay);
            comment = (AutoLinearLayout) itemView.findViewById(R.id.order_home_item_suggestion);
        }
    }
}
